(function( global,$) {
	var ids = 0; //公共序号
	global.jForest = {
		_modals:[],
		getId:function(){return (ids++);},
		getAjax:function(options){return new Ajax(options);},
		request:function(url){if(url){global.document.location.href=url;}},
		isNotNull:function(value){return value != null && value != 'undefined'},
		replaceClass:function(el,oldClass,newClass){$(el).removeClass(oldClass);$(el).addClass(newClass);},
		alert:function(title,msg,click){
			jf.alertFlag=false,click=$.isFunction(click)?click:function(flag){};
			if(!jf.alertModal){jf.alertModal=$('<div class="modal fade" tabindex="-1"><div class="modal-dialog" style="width:400px;">'
				+'<div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal">&times;</button>'
				+'<h4 class="modal-title">'+title+'</h4></div><div class="modal-body">'+msg+'</div>'
				+'<div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">取消</button>'
				+'<button type="button" class="btn btn-primary">确定</button></div></div></div></div>');
			jf.alertModal.find('.btn-primary').click(function(){jf.alertFlag=true,jf.alertModal.modal('hide');});}
			else{jf.alertModal.find('.modal-title').html(title);jf.alertModal.find('.modal-body').html(msg);}
			jf.alertModal.one('hidden.bs.modal',function(){click.call(this,jf.alertFlag)});jf.alertModal.modal('show');},
		error:function(title,msg){
			if(!msg){msg=title,title="系统错误";}
			var $el = $('<div class="modal fade in" style="display:block;"><div class="alert alert-danger" style="max-width:30%;margin:auto;position:relative;top:70px;">'
				+'<strong>'+title+'</strong>&nbsp;'+msg+'</div></div>');
			var mask = jf.mask();
			$el.click(function(){$el.remove();mask.remove();
			}).appendTo('body');
		},
		info:function(title,msg){
			if(!msg){msg=title,title="系统提示";}
			var $el = $('<div class="modal fade in" style="display:block;"><div class="alert alert-info" style="max-width:30%;margin:auto;position:relative;top:70px;">'
				+'<strong>'+title+'</strong>&nbsp;'+msg+'</div></div>');
			var mask = jf.mask();
			$el.click(function(){$el.remove();mask.remove();
			}).appendTo('body');
		},
		modal:function(title,url,data,callback){
			if($.isFunction(data)){callback=data;data={};}
			var modal=jf._modals.shift();if(!modal){modal=new Modal();}
			modal.load(title,url,data,callback);
		},
		modalCallback:function(el,result){
			var modalEl=$(el).parents('.modal:first'),callback=modalEl.data('callback');
			if($.isFunction(callback)){callback.apply(modalEl,[result]);}
		},
		mask:function(click){
			var $el = $('<div class="modal-backdrop fade in"></div>').appendTo('body');
			if($.isFunction(click)){$el.click(function(e){click.apply($el,[e])});}return $el;
		},
		//美化标签
		nice:function(){
			// <label class="nice-checkbox js"><input type="checkbox"></label>
			$('.nice-checkbox input[type="checkbox"]').each(function(i,el){
				if(el.checked){$(el).parent().addClass('on');}
			});
			$('.nice-checkbox.js').mousedown(function(event){
				$(this).toggleClass('on');
			});
		}
	}
	
	var Modal=function(){
		this.$el=$('<div class="modal fade" tabindex="-1"><div class="modal-dialog"><div class="modal-content">'
			+'<div class="modal-header"><button type="button" class="close" data-dismiss="modal">&times;</button>'
			+'<h4 class="modal-title"></h4></div><div class="modal-body"></div></div></div></div>');
		var modal=this;this.$el.on('hidden.bs.modal',function(e){jf._modals.push(modal);}).appendTo($('body'));
	}
	
	Modal.prototype = {
		load:function(title,url,data,callback){
			if($.isFunction(callback)){this.$el.data('callback',callback);}
			this.$el.find('.modal-title').html(title);var modal=this;
			this.$el.find('.modal-body').load(url,data,function(){
				modal.$el.modal('show');
			});
		}
	}
	
	var Ajax = function(options){
		this.options = $.extend({
			bindObj:null,	//绑定的对象
			method:'post',
			dataType:'json',
			url:'',
			param:null,  //方法或对象
			callback:function(){}
		},options);
		this.loadedParam = {}; //上一次加载时用的参数
	}
	
	Ajax.prototype = {
		getParam:function(param){
			if($.isFunction(param)){return param.apply(this.options.bindObj||this,[this.loadedParam]);
			}else if(param){return param;} else if($.isFunction(this.options.param)){
				return this.options.param.apply(this.options.bindObj||this,[this.loadedParam]);
			}else{return this.options.param||{};}
		},
		getCallback:function(callback){
			if($.isFunction(callback)){return callback;}
			else if($.isFunction(this.options.callback)){return this.options.callback;}
			else{return function(result){};}
		},
		isValid:function(){return this.options.url?true:false;},
		load:function(param,callback){
			var obj = this.options.bindObj||{},opts=this.options;
			this.loadedParam = this.getParam(param);callback = this.getCallback(callback);
			$.ajax({url:opts.url,data:this.loadedParam,dataType:opts.dataType,type:opts.method,
				success:function(result){callback.apply(obj,[result||{}]);}})
		}
	}
	
	global.jf = global.jForest;
})(typeof window !== "undefined" ? window : this,jQuery);